package com.shumm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shumm.common.Result;
import com.shumm.mapper.TcmrelationMapper;
import com.shumm.po.Tcmrelation;
import com.shumm.service.TcmrelationService;
import com.shumm.utils.PageUtil;
import com.shumm.vo.TcmAndTcmrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shumm
 * @Description
 * @create 2023-11-18 13:17
 */
@Service
//@SuppressWarnings("all")
public class TcmrelationServiceImpl implements TcmrelationService {

    @Autowired
    private TcmrelationMapper mapper;

    @Override
    public Result add(Tcmrelation tcmr) {
        if (mapper.isExistedById(tcmr.getId()) <= 0) {
            mapper.add(tcmr);
            return Result.success();
        }
        return Result.error("该id已存在");
    }

    @Override
    public Result deleteById(Integer id) {
        if (mapper.delete(id) > 0) {
            return Result.success();
        }
        return Result.error("该id不存在");
    }

    @Override
    public Result update(Tcmrelation tcmr) {
        if (mapper.isExistedById(tcmr.getId()) > 0) {
            mapper.update(tcmr);
            return Result.success();
        }
        return Result.error("该id不存在");
    }

    @Override
    public Result selectById(Integer id) {
        Tcmrelation tcmr = mapper.selectById(id);
        return Result.success(tcmr);
    }

    @Override
    public Result list(int num, int size) {
        PageHelper.startPage(num, size);
        Page<TcmAndTcmrVO> tcmAndTcmrVOS = (Page) mapper.listWithTcmName();
        return Result.success(tcmAndTcmrVOS, new PageUtil(tcmAndTcmrVOS.getPageNum(), tcmAndTcmrVOS.getPageSize(), tcmAndTcmrVOS.getTotal()));
    }

    @Override
    public Result listByCondition(String name, String chuchu, String content, int num, int size) {
        PageHelper.startPage(num, size);
        Page<TcmAndTcmrVO> tcmAndTcmrVOS = (Page) mapper.selectByCondition(name, chuchu, content);
        return Result.success(tcmAndTcmrVOS, new PageUtil(tcmAndTcmrVOS.getPageNum(), tcmAndTcmrVOS.getPageSize(), tcmAndTcmrVOS.getTotal()));
    }

    //B）心得列表应显示心得和该心得归属的草药名称。
    @Override
    public Result contentList() {
        PageHelper.startPage(1, 10);
        Page<TcmAndTcmrVO> tcmAndTcmrVOS = (Page) mapper.contentList();
        return Result.success(tcmAndTcmrVOS);
    }
}
